package com.shushan.mixedexam.strangewords;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.pages.newtype.bean.StrangeWordsPhraseItemBean;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: StrangeWordsStudyAdapter
 * @Desciption: //StrangeWordsStudyFragment下recyclerview用到的适配器
 * @author: zhangshihao
 * @date: 2018-06-21
 */
public class StrangeWordsStudyAdapter extends RecyclerView.Adapter<StrangeWordsStudyAdapter.ViewHolder>{

    private List<StrangeWordsPhraseItemBean> phraseItemBeans;
    private OnTrumpetClickListener onTrumpetClickListener;

    public StrangeWordsStudyAdapter(List<StrangeWordsPhraseItemBean> phraseItemBeans){
        this.phraseItemBeans = phraseItemBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_strange_word_study,null,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        StrangeWordsPhraseItemBean phraseItemBean = phraseItemBeans.get(position);
        holder.tvSpell.setText(phraseItemBean.pinyin);
        holder.ivTrumpet.setImageResource(R.drawable.anim_audio_play_small);
        holder.tvMeaningDesc.setText("释义："+phraseItemBean.shiyi);
        final AnimationDrawable playAnim = (AnimationDrawable) holder.ivTrumpet.getDrawable();
        holder.spellLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onTrumpetClickListener != null) {
                    onTrumpetClickListener.onTrumpetClick(position,playAnim);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return phraseItemBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout spellLayout;
        private ImageView ivTrumpet;
        private TextView tvSpell;
        private TextView tvMeaningDesc;
        public ViewHolder(View itemView) {
            super(itemView);
            spellLayout = itemView.findViewById(R.id.ll_spell);
            tvSpell = itemView.findViewById(R.id.tv_spell);
            ivTrumpet = itemView.findViewById(R.id.iv_trumpet);
            tvMeaningDesc = itemView.findViewById(R.id.tv_meaning_desc);
        }
    }

    public void setOnTrumpetClickListener(OnTrumpetClickListener onTrumpetClickListener) {
        this.onTrumpetClickListener = onTrumpetClickListener;
    }

    public interface OnTrumpetClickListener{
        void onTrumpetClick(int position,AnimationDrawable playAnim);
    }

}